﻿
namespace Behemoth.SharePoint.Branding {

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;

    public class ThemeTools {

        public const string DEFAULT_THEME_NAME = "none";

        public enum ModificationType {
            Add,
            Remove
        }

        public static void ModifySPTheme(
            ModificationType type, 
            string themeName, 
            string themeDisplayName,
            string themeDescription,
            string themePreviewImageName
            ) {
            XDocument doc = null;
            XNamespace ns = "http://tempuri.org/SPThemes.xsd";

            // path to the SPTHEMES.XML file
            string spthemePath = Path.Combine(SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\1033"), "SPTHEMES.XML");
            string contents = string.Empty;

            // read the contents of the SPTHEMES.XML file
            using (StreamReader streamReader = new StreamReader(spthemePath))
            {
                contents = streamReader.ReadToEnd();
                streamReader.Close();
            }

            using (StringReader stringReader = new StringReader(contents.Trim()))
            {
                // create a new XDocument from the contents of the file
                doc = XDocument.Load(stringReader);

                // retrieve all elements with a TemplateID of our theme.  At most, there should only be one
                var element = from b in doc.Element(ns + "SPThemes").Elements(ns + "Templates")
                              where b.Element(ns + "TemplateID").Value == themeName
                              select b;

                bool exists = (element != null && element.Count() > 0);

                if (type == ModificationType.Add)
                {
                    if (!exists)
                    {
                        // create an XElement that defines our custom theme
                        XElement xml =
                            new XElement(ns + "Templates",
                                new XElement(ns + "TemplateID", themeName),
                                new XElement(ns + "DisplayName", string.Format("{0} Site Theme", themeDisplayName)),
                                new XElement(ns + "Description", string.Format("{0}", themeDescription)),
                                new XElement(ns + "Thumbnail", string.Format("1033/IMAGES/{0}/{1}", themeName, themePreviewImageName)),
                                new XElement(ns + "Preview", string.Format("1033/IMAGES/{0}/{1}", themeName, themePreviewImageName)));

                        // add the element to the file and save
                        doc.Element(ns + "SPThemes").Add(xml);
                        doc.Save(spthemePath);
                    }
                }
                else
                {
                    if (exists)
                    {
                        // if the element exists, remove it and save
                        element.Remove();
                        doc.Save(spthemePath);
                    }
                }

                stringReader.Close();
            }
        }

    } // class
} // namespace
